package com.zr.controller;

import com.zr.entity.Dept;
import com.zr.entity.Emp;
import com.zr.entity.Userinfo;
import com.zr.mapper.DeptMapper;
import com.zr.mapper.EmpMapper;
import com.zr.mapper.UserinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ：Zhang
 * @date ：Created in 2024/12/19 10:52
 * @description：
 * @modified By：
 * @version:
 */
@Controller
@RequestMapping("/userinfo")
public class UserController {
    @Autowired
    private UserinfoMapper userinfoMapper;
    @RequestMapping("/login")
    public String login(String name, String password, HttpSession session){
        Userinfo userinfo1 = userinfoMapper.selectByNameAndPwd(name, password);
        if(userinfo1 != null){
            return "redirect:/emp/list";
        }else{
            return "redirect:/login.jsp";
        }
    }
    @RequestMapping("register")
    public String register(Userinfo userinfo){
        /*判断接受的数据符合条件*/
        System.out.println(userinfo.getName());
        if(userinfo.getName().length()>0){
            if(userinfo.getPassword().length()>0){
                List<Userinfo> emp = userinfoMapper.selectByName(userinfo.getName());
                System.out.println(emp);
                if(emp!=null){
                    return "redirect:/register.jsp?error=error";
                }
                userinfoMapper.insert(userinfo);
                return "redirect:/login.jsp";
            }
        }
        return "redirect:/register.jsp?error='用户名重复或用户名及密码格式有误'";
    }
}
